package com.bloveoa.zkkd.util;

import com.bloveoa.zkkd.doman.OutTokenModel;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.NamedThreadLocal;

/**
 * @author lihe
 */
@UtilityClass
@Slf4j
public class SessionUtils {
    /**
     * 用于持有用户会话
     */
    @SuppressWarnings("AlibabaThreadLocalShouldRemove")
    private static final ThreadLocal<OutTokenModel> SESSION_THREAD_LOCAL = new NamedThreadLocal<>("session thread local");

    /**
     * 获取当前用户的会话信息
     *
     * @return 会话信息
     */
    public static OutTokenModel getSession() {
        OutTokenModel session = SESSION_THREAD_LOCAL.get();
        log.trace("current session is {}", session);
        return session;
    }

    /**
     * 设置当前线程的会话信息
     *
     * @param session 新的session
     */
    public static void setSession(OutTokenModel session) {
        SESSION_THREAD_LOCAL.set(session);

    }

    public static Integer getUid() {
        return getSession().getId();
    }


    public static void remove() {
        SESSION_THREAD_LOCAL.remove();
    }
}
